<?php
// +----------------------------------------------------------------------
// | Base.
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.abc3210.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: wjf <admin@abc3210.com> 2017/6/27
// +----------------------------------------------------------------------

namespace wjf\controller;


use think\Config;
use think\Controller;
use think\Request;
use think\View;

class Base extends Controller
{
    public function __construct(Request $request = null)
    {
        if (wjf_is_installed() && $request->module() != 'install') {
            header('Location:' . wjf_get_root() . 'index.php?s=install');
            exit();
        }

        if (is_null($request)) {
            $request = Request::instance();
        }
        $this->request = $request;

        $this->_initializeView();
        $this->view = View::instance(Config::get('template'), Config::get('view_replace_str'));

        //控制器初始化
        $this->_initialize();

        //前置操作
        if ($this->beforeActionList) {
            foreach ($this->beforeActionList as $method => $options) {
                is_numeric($method) ? $this->beforeAction($options) : $this->beforeAction($method, $options);
            }
        }
    }

    //初始化试图配置
    protected function _initializeView()
    {
    }

    /**
     * 排序 排序字段为list_orders数组 POST 排序字段为：list_order
     * @param $model
     * @return bool
     */
    protected function listOrders($model)
    {
        if (!is_object($model)) {
            return false;
        }

        $pk = $model->getPk(); //获取主键名称
        $ids = $this->request->post("list_orders/a");

        if (!empty($ids)) {
            foreach ($ids as $key => $r) {
                $data['list_order'] = $r;
                $model->where([$pk => $key])->update($data);
            }

        }

        return true;
    }
}